package com.suixun.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.controller.BaseManager;
import com.google.gson.Gson;
import com.suixun.entity.form.StudentForm;
import com.suixun.entity.page.PageModel;
import com.suixun.service.MonitorService;
import com.suixun.service.StudentService;
import com.suixun.util.Constant;
import com.suixun.util.StringUtil;
import com.suixun.vo.MonitorVO;
import com.suixun.vo.SafeRegionVO;
import com.suixun.vo.StudentVO;

@Controller
@RequestMapping("/monitorManager")
public class MonitorController extends BaseManager {
	@Resource
	private MonitorService monitorService;
	@Resource
	private StudentService studentService;
	/**
	 * 
	 * <p>Title: queryMonitorList</p>
	 * <p> 功能描述:查询列表监控 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryMonitorList.json")
	public Object queryMonitorList(String param){
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			MonitorVO monitor=new Gson().fromJson(param,MonitorVO.class);
			StudentForm student=new StudentForm();
			if(monitor!=null){
				student.setMdtid(monitor.getMdtid());
				student.setSimid(monitor.getSimid());
				student.setPagecount(monitor.getPagecount());
				student.setPagenumber(monitor.getPagenumber());
				student.setSchoolname(monitor.getSchoolname());
				student.setType(1);
			}
			PageModel<StudentVO> studentVOs=studentService.queryList(student);
			if(studentVOs!=null){
				if(studentVOs.getQueryresult().size()>0){
					List<Integer> objectidList=new LinkedList<>();
					for(StudentVO objectinfo:studentVOs.getQueryresult()){
						objectidList.add(Integer.parseInt(objectinfo.getObjectid()));
					}
					if(monitor==null){
						monitor=new MonitorVO();
					}
					monitor.setTotlepage(studentVOs.getTotlepage());
					monitor.setTotlerows(studentVOs.getTotlerows());
					monitor.setCurrentpage(studentVOs.getCurrentpage());
					monitor.setObjectidList(objectidList);
					monitor.setObjectList(studentVOs.getQueryresult());
					return success(monitorService.selectMonitorList(monitor));
				}
			}
			return success(null);
		} catch (Exception e) {
			logger("MonitorController queryMonitorList:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	/**
	 * 
	 * <p>Title: queryMapMonitorList</p>
	 * <p> 功能描述:查询地图监控 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryMapMonitorList.json")
	public Object queryMapMonitorList(String param){
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			MonitorVO monitor=new Gson().fromJson(param,MonitorVO.class);
			if(monitor==null){
				return success(monitorService.selectMapMonitorList(monitor));
			}else{
				StudentForm student=new StudentForm();
				student.setMdtid(monitor.getMdtid());
				student.setSimid(monitor.getSimid());
				student.setSchoolname(monitor.getSchoolname());
				student.setType(1);
				student.setIsNotPage("1");
				PageModel<StudentVO> studentVOs=studentService.queryList(student);
				if(studentVOs!=null){
					if(studentVOs.getQueryresult().size()>0){
						List<Integer> objectidList=new LinkedList<>();
						for(StudentVO objectinfo:studentVOs.getQueryresult()){
							objectidList.add(Integer.parseInt(objectinfo.getObjectid()));
						}
						monitor.setObjectidList(objectidList);
						monitor.setObjectList(studentVOs.getQueryresult());
						return success(monitorService.selectMapMonitorList(monitor));
					}
				}
				return success(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger("MonitorController queryMapMonitorList:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 
	 * <p>Title: queryMapMonitorsByFollow</p>
	 * <p> 功能描述:跟踪历史轨迹 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryMapMonitorsByFollow.json")
	public Object queryMapMonitorsByFollow(String param){
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			MonitorVO monitor=new Gson().fromJson(param,MonitorVO.class);
			if(monitor==null||!StringUtil.isNotEmpty(monitor.getObjectid())){
				return fail(Constant.EMPTY);
			}
			return success(monitorService.selectMonitorFollow(monitor));
		} catch (Exception e) {
			logger("MonitorController queryMapMonitorsByFollow:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	/** 
	 * 
	 * <p>Title: querySafeRegionList</p>
	 * <p> 功能描述:查询安全区域列表 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/querySafeRegionList.json")
	public Object querySafeRegionList(String param){
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			MonitorVO monitor=new Gson().fromJson(param,MonitorVO.class);
			if(monitor==null){
				return fail(Constant.EMPTY);
			}
			return success(monitorService.querySafeRegionList(monitor));
		} catch (Exception e) {
			e.printStackTrace();
			logger("MonitorController querySafeRegionList:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	/**
	 * 
	 * <p>Title: addSafeRegion</p>
	 * <p> 功能描述:添加安全区域 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addSafeRegion.json")
	public Object addSafeRegion(String param){
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			SafeRegionVO region=new Gson().fromJson(param,SafeRegionVO.class);
			if(region==null||!StringUtil.isNotEmpty(region.getRegiontype())||!StringUtil.isNotEmpty(region.getRegionname())||
					!StringUtil.isNotEmpty(region.getRegionaddr())||!StringUtil.isNotEmpty(region.getRadius())||!StringUtil.isNotEmpty(region.getType())){
				return fail(Constant.EMPTY);
			}
			region.setCreateuser(String.valueOf(getUserId()));
			int isAdd=monitorService.addSafeRegion(region);
			Map<String,String> result=new HashMap<>();
			if(isAdd==-1){
				return fail(Constant.EMPTY);
			}else{
				result.put("result", Constant.OPERATE_SUCCESS);
				return success(result);
			}
		} catch (Exception e) {
			logger("MonitorController addSafeRegion:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 
	 * <p>Title: updateSafeRegion</p>
	 * <p> 功能描述:修改安全区域 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateSafeRegion.json")
	public Object updateSafeRegion(String param){
		boolean bln=checkRight();
		if(!bln)
			return fail(Constant.Authority.CHECK_FALSE);
		SafeRegionVO region=new Gson().fromJson(param,SafeRegionVO.class);
		if(region==null||!StringUtil.isNotEmpty(region.getRegionid())){
			return fail(Constant.EMPTY);
		}
		region.setCreateuser(String.valueOf(getUserId()));
		try {
			int isUpdate=monitorService.updateSafeRegion(region);
			if(isUpdate==1){
				return success(Constant.OPERATE_SUCCESS);
			}else{
				return fail(Constant.OPERATE_FAIL);
			}
		} catch (Exception e) {
			logger("MonitorController updateSafeRegion:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 
	 * <p>Title: deleteSafeRegion</p>
	 * <p> 功能描述:删除安全区域 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteSafeRegion.json")
	public Object deleteSafeRegion(String param){
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			SafeRegionVO region=new Gson().fromJson(param,SafeRegionVO.class);
			if(region==null||!StringUtil.isNotEmpty(region.getRegionid())){
				return fail(Constant.EMPTY);
			}
			int isDelete=monitorService.deleteSafeRegion(region);
			if(isDelete==1){
				return success(Constant.OPERATE_SUCCESS);
			}else{
				return fail(Constant.OPERATE_FAIL);
			}
		} catch (Exception e) {
			logger("MonitorController deleteSafeRegion:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
}
